import java.net.*;
import java.io.*;
import java.util.*;

public class Server {

	private ServerSocket socket;
	private int maxPlayers;
	private static PlayerThread[] players;
	private Table newTable;
	
	
	Server(int port, int initialCash, int smallBlind, int bigBlind, int maxPlayers){
		
		newTable = new Table(smallBlind, bigBlind, initialCash, maxPlayers);
		socket=null;
		
		try {
			socket = new ServerSocket(port); 
		}
		catch (IOException e) {
			System.out.println("Failed to listen on port "+port);
			System.exit(-1);
		}
				
		this.maxPlayers=maxPlayers;
		players = new PlayerThread[maxPlayers];
		
	}
	
	private void listen(){
		System.out.println("Now listening...");
		Socket clientSocket = null;
		boolean added=false;
		System.out.println(newTable.gameOn());
		try{
			clientSocket = socket.accept();		
		}
		catch(IOException e2){
			System.out.println("Connection could not be established");
			System.exit(-1);
		}
		
		int i=0;
		
		PlayerThread player = new PlayerThread(clientSocket, players);
		
		while((added==false)&&(i<maxPlayers)){
			if((players[i]==null)){
				players[i]=player;
				Thread request = new Thread(player);
				request.start();
				added=true;
				player.setId(i);
				player.setTable(newTable);
				
			} else i++;
			

			
			System.out.println("i : "+i+"added: "+added);
		}
		
		if(newTable.gameOn()==true){
			player.setTable(newTable);
			System.out.println("Game is on");
			player.sendMessage("***Game is currently on. Try again later***");
			player.sendMessage("***Bye***");
			player.close();
		} 

		
		if(i==maxPlayers-1){
			newTable.addPlayers(players);	
			Thread table = new Thread(newTable);
			table.start();
		}
		
		
		if(i==maxPlayers) {
			System.out.println("A player over limit");
			player.sendMessage("***This room is full. Try again later***");
			player.sendMessage("***Bye***");
			player.close();
		}
		
		
	}
	
	public void start(){
		System.out.println("Server started");
		while(true){
			listen();
		}
	}
	
	public static void main(String args[]){
		Scanner scanner = new Scanner(System.in);
		
		int max;
		int port;
		int smallBlind;
		int bigBlind;
		int initialCash;

		
		try{
			System.out.println("State port number");
			port=scanner.nextInt();
			
			System.out.println("State maximum players number");
			max=scanner.nextInt();
			
			while(max<=1&&max>6){
				System.out.println("Number of players must be between 2 and 6");
				max=scanner.nextInt();
			}
			
			System.out.println("State initial cash");
			initialCash=scanner.nextInt();
			
			while(initialCash<=3&&initialCash>=(Integer.MAX_VALUE/max)){
				System.out.println("Initial cash cannot be less than 3 and more than "+(Integer.MAX_VALUE/max)+". State initial cash");
				initialCash=scanner.nextInt();
			}
			
			
			System.out.println("State small blind");
			smallBlind=scanner.nextInt();
			
			while(smallBlind>=initialCash){
				System.out.println("Small blind cannot be bigger than initial cash! State small blind");
				smallBlind=scanner.nextInt();
			}
			
			System.out.println("State big blind");
			bigBlind=scanner.nextInt();
			
			while((bigBlind<=smallBlind)&&(bigBlind>=initialCash)){
				System.out.println("Big blind must be set between small blind and initial cash! Try again.");
				bigBlind=scanner.nextInt();
			}
			
		}
		catch(InputMismatchException e){
			scanner.close();
			return;
		}
		scanner.close();
		
		Server server = new Server(port,initialCash, smallBlind, bigBlind, max);
		server.start();

		
		
	}
	
	public void finalize(){
		
		for(int i=0; i<maxPlayers; i++){
			if(players[i]!=null){
				players[i].sendMessage("***Bye***");
				players[i].close();
			}

		}
	
		try { 
			socket.close();
		}
		catch(IOException e) {
			System.out.println("Could not close ServerSocket");
		}
	}
	
	
}
